﻿using MediatR;
using WeatherHistory.Data.Repositories.Interfaces;
using WeatherHistory.Shared.Exceptions;

namespace WeatherHistory.Api.CommandsQueries.Commands.Forecasts.Delete;

public class DeleteForecastsCommandHandler : IRequestHandler<DeleteForecastsCommandRequest>
{
    private readonly IForecastRepository _forecastRepository;

    public DeleteForecastsCommandHandler(IForecastRepository forecastRepository)
    {
        _forecastRepository = forecastRepository;
    }

    public async Task Handle(DeleteForecastsCommandRequest request, CancellationToken cancellationToken)
    {
        var forecast = await _forecastRepository.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
        if (forecast == null) throw new NotFoundException($"Прогноз погоды с id '{request.Id}' не найден");

        await _forecastRepository.DeleteAsync(forecast, cancellationToken);
    }
}